package com.jingwu.example.service

import org.springframework.context.annotation.Import
import org.springframework.test.context.ContextConfiguration
import spock.lang.Specification

import javax.annotation.Resource

/**
 * spock 通过 @ContextConfiguration 来加载上下文
 * 即 Junit 5 的  @ExtendWith(SpringExtension.class)
 * @see <ahref="https://docs.spring.io/spring-boot/docs/1.2.0.RC1/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-with-spock"      >      Using Spock to test Spring Boot applications</a>
 * @author 菁芜
 * @since 2021/11/20
 */
@Import(FooService.class)
@ContextConfiguration
class FooServiceTest extends Specification {

    @Resource
    FooService service;

    def "add"() {
        given:
        def i = 1
        when:
        i++
        then: "aa"
        i == 2
    }

    def "add test: #a + #b = #result"() {
        expect: "加法测试"
        service.add(a, b) == result

        where: "边界"
        a  | b  | result
        1  | 2  | 3
        2  | 2  | 4
        10 | 10 | 20

    }

    def "add test2"() {
        expect: "加法测试"
        // idea 要求确定具体类型
        service.add(a as int, b as int) == result

        where: "where 的另一种写法（可能是根据索引来确定值的）"
        a << [1, 2, 10]      // 索引为 0 时 ：a = 1, b = 2, c = 3
        b << [2, 2, 10]      // 索引为 1 时 ：a = 2, b = 2, c = 4
        result << [3, 4, 20] // 索引为 0 时 ：a = 10, b = 10, c = 20

    }

    def "max test"() {
        given:
        def numbers = [2, 1, -10, 20, 1000]

        when:
        max = service.max(numbers)

        then: "不抛异常"
        noExceptionThrown()

        where:
        max << 100
    }

    def "max capture exception test"() {
        when:
        service.max(null)

        then: "抛异常"
//        thrown(IllegalArgumentException)
        IllegalArgumentException e = thrown()
        e.message == "参数异常"
    }

}
